package com.peanut.baby.mvp.point;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.peanut.baby.R;
import com.peanut.baby.http.HttpConfig;
import com.peanut.baby.model.PointHistory;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.web.WebViewActivity;
import com.peanut.devlibrary.widget.TitleLayout;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by anonymouslystudio on 2017/8/7.
 */

public class PointRecordActivity extends BaseMvpActivity implements TitleLayout.OnTitleClickListener,
    PullRecyclerView.OnRecyclerRefreshListener,PointRecordContract.View{
    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.point_list)
    PullRecyclerView rulesList;

    private int pageNo = 1;
    private int pageSize = 10;
    private List<PointHistory> data;
    private PointRecordRecyclerAdapter adapter;
    private PointRecordPresenter presenter;

    public static void start(Context ctx){
        ctx.startActivity(new Intent(ctx,PointRecordActivity.class));
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_point_record);
        ButterKnife.bind(this);
        presenter = new PointRecordPresenter(this,this);
        initView();
    }

    private void initView(){
        title.setTitle("积分记录");
        title.setOnTitleClickedListener(this);
        title.setTitleRightText("积分规则");
        title.setTitleRightVisibility(true);
        data = new ArrayList<>();

        rulesList.enableLoadMore(true);
        rulesList.enablePullRefresh(true);
        rulesList.setOnRecyclerRefreshListener(this);

        adapter = new PointRecordRecyclerAdapter(this,data);
        rulesList.setLayoutManager(new XLinearLayoutManager(this));
        rulesList.setAdapter(adapter);

        rulesList.postRefreshing();
    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {
//        PointRulesActivity.start(this);
        WebViewActivity.start(this, HttpConfig.POINT_RULE_URL,"积分规则","");
    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onPullRefresh() {
        pageNo = 1;
        presenter.getRecordList(pageNo,pageSize);
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        presenter.getRecordList(pageNo,pageSize);
    }

    @Override
    public void onRecordListGet(List<PointHistory> _data) {
        stopRefreshOrLoading();
        rulesList.enableLoadMore(_data != null && _data.size() >= pageSize);
        if(pageNo == 1){
            data.clear();
        }
        if(_data != null && !_data.isEmpty()){
            data.addAll(_data);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetRecordFailed(String reason) {
        stopRefreshOrLoading();
        rulesList.enableLoadMore(false);
        showToast(reason);
    }

    private void stopRefreshOrLoading(){
        rulesList.stopRefresh();
        rulesList.stopLoadMore();
    }
}
